/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import fr.ove.utils.ConnectionConfigurable;
/**
* A connection parameter setter.
*/
public class ConnectionConfigDialog extends JDialog {
    /**
    * A connection configurable object.
    */
    private ConnectionConfigurable configurable;
    
    /**
    * The textfield for entering the host name.
    */
    private JTextField entryHost;
    
    /**
    * The textfield for entering the port number.
    */
    private JTextField entryPort;
    
    
    /**
    * The Constructor.
    * @param configurableObj a connection configurable object.
    */
    public ConnectionConfigDialog(JFrame frame, ConnectionConfigurable configurableObj) {
        super(frame, "Connection Configuration", true);
        configurable = configurableObj;
        
        JPanel contentPane = new JPanel(new GridBagLayout());
        
        TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "");
        //border.setInsets(new Insets(10, 10, 10, 10));
        contentPane.setBorder(border);
        
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.weightx = 1.0;
		constraints.weighty = 1.0;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		
		JLabel label = new JLabel("Host Name : ", SwingConstants.RIGHT);
		constraints.anchor = GridBagConstraints.WEST;
		contentPane.add(label, constraints);
		
		label = new JLabel("Port Number : ", SwingConstants.RIGHT);
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.anchor = GridBagConstraints.WEST;
		contentPane.add(label, constraints);
		
		entryHost = new JTextField(10);
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.fill = GridBagConstraints.NONE;
        constraints.insets = new Insets(0, 10, 0, 0);
		constraints.anchor = GridBagConstraints.EAST;
		contentPane.add(entryHost, constraints);
		
		entryPort = new JTextField(4);
		constraints.gridy = 1;
		constraints.anchor = GridBagConstraints.WEST;
		contentPane.add(entryPort, constraints);
		
        Box panelButtons = new Box(BoxLayout.X_AXIS);
        JButton button = new JButton("OK");
        button.setToolTipText("Confirm data values");
        button.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    configurable.setHostName(entryHost.getText());
                    try {
                        int port = Integer.parseInt(entryPort.getText());
                        configurable.setPortNumber(port);
                        hide();
                    }
                    catch (NumberFormatException nfe) {
                        JOptionPane jop = new JOptionPane("String invalid port number : " + entryPort.getText(),
                                        JOptionPane.ERROR_MESSAGE,
                                        JOptionPane.DEFAULT_OPTION);
                                        
                        JDialog dialog = jop.createDialog(ConnectionConfigDialog.this, "Invalid port Number");
                        dialog.show();
                    }
                }
            }
        );
        panelButtons.add(button);
        
        panelButtons.add(Box.createHorizontalGlue());
        
        button = new JButton("Cancel");
        button.setToolTipText("Ignore data values");
        button.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    hide();
                }
            }
        );
        panelButtons.add(button);
        
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.gridwidth = 2;
        constraints.insets = new Insets(15, 0, 0, 0);
        
		contentPane.add(panelButtons, constraints);
        
		setContentPane(contentPane);
                
        pack();
    }
    
    /**
    * Returns the connection configurable object.
    */
    public ConnectionConfigurable getConnectionConfigurable() {
        return configurable;
    }
    
    /**
    * Sets the connection configurable object
    * @param configurable the connection configurable object.
    */
    public void setConnectionConfigurable(ConnectionConfigurable configurable) {
        this.configurable = configurable;
    }
    
    /**
    * Shows the instance.
    */
    public void show() {
        entryHost.setText(configurable.getHostName());
        entryPort.setText("" + configurable.getPortNumber());
        super.show();
    }
}

